package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.ShareIncome;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.ShareIncomeService;
import co.yixiang.modules.system.dto.ShareIncomeDto;
import co.yixiang.modules.system.dto.queryCriteria.ShareIncomeQueryCriteria;
import co.yixiang.modules.system.mapper.ShareIncomeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lww
* @date 2020-10-22
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "ShareIncome")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShareIncomeServiceImpl extends BaseServiceImpl<ShareIncomeMapper, ShareIncome> implements ShareIncomeService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(ShareIncomeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ShareIncome> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(ShareIncomeDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<ShareIncome> queryAll(ShareIncomeQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ShareIncome.class, criteria));
    }


    @Override
    public void download(List<ShareIncomeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShareIncomeDto ShareIncome : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商铺唯一主键", ShareIncome.getStoreUnique());
            map.put("订单号", ShareIncome.getOrderNumber());
            map.put("分润比例", ShareIncome.getStoreScale());
            map.put("分润前的金额", ShareIncome.getBeforeMoney());
            map.put("收入金额，即分润后金额", ShareIncome.getMoney());
            map.put("创建时间", ShareIncome.getCreateTimes());
            map.put("更新时间", ShareIncome.getUpdateTimes());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
